package org.example.smartEducationBackend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.smartEducationBackend.entity.DTO.UserDTO;
import org.example.smartEducationBackend.entity.POJO.RestResult;
import org.example.smartEducationBackend.entity.POJO.UserAccount;
import org.example.smartEducationBackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;  // 添加这行

import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
@Tag(name = "User", description = "用户相关接口")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    @Operation(summary = "用户登录接口")
    public RestResult<Map<String, Object>> login(@RequestBody UserDTO userDTO){
        Map<String, Object> user = userService.getUser(userDTO);

        return RestResult.ok(user);
    }

    @GetMapping("/checkUsername")
    public RestResult<UserAccount> checkUsername(String username){
        UserAccount userAccount = userService.getuserByName(username);
        return RestResult.ok(userAccount);

    }
    @PostMapping("/register")
    public RestResult<String> register(@RequestBody UserAccount userAccount){
        userService.insert(userAccount);
        return RestResult.ok("success");
    }


    @GetMapping("/getInfo")
    @Operation(summary = "用户信息获取接口")
    public RestResult<UserAccount> getInfo(Integer userId){
        UserAccount user = userService.getUserById(userId);

        return RestResult.ok(user);
    }


    @PostMapping("/saveImg")
    @Operation(summary = "用户头像上传接口")
    public RestResult<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        String avatarUrl = userService.saveUserAvatar(file);
        return RestResult.ok(avatarUrl);
    }

    @PutMapping("/update/{id}")
    public RestResult<String> update(@PathVariable Integer id,@RequestBody UserAccount userAccount){
        userAccount.setUserId(id);
        System.out.println(userAccount);
        userService.update(userAccount);





        return RestResult.ok("success");
    }
}
